﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Security.Cryptography;
using ShareMarking.Service;
using ShareMarking.Entity;
using ShareMarking.Common;
using System.Data;

namespace ShareMarking.ShowqrCode
{
    /// <summary>
    /// api 的摘要说明
    /// </summary>
    public class api : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            var method = ComLib.QueryStringHelper.GetFormString("method");
             if (method == "")
             {
                string action = DTRequest.GetQueryString("action");
                switch (action)
                {
                    case "getpnrlistbyec":
                        getPnrListByEC(context);//EC接口
                        break;
                    case "getpnrqrlist2":
                        getpnrqrlist2(context);//测试PNR接口
                        break;
                    case "getqrbysceneid":
                        getqrbysceneid(context);//ECRM接口
                        break;
                    case "getqrbysceneidTest":
                        getqrbysceneidTest(context);//测试ECRM接口
                        break;
                    default:
                        context.Response.Write("{\"subcode\":\"05\",\"msg\":\"请求方法错误\",\"result\":{}}");
                        break;
                }
             }
             else
             {
                 switch (method)
                 {
                     case "gethospqrlist":
                         gethospqrlist(context);//正式接口
                         break;
                     case "getpnrqrlist":
                         getpnrqrlist(context);//正式接口
                         break;
                     case "getqrbysceneid":
                         getqrbysceneid(context);//ECRM接口
                         break;
                     case "getpnrlistbyec":
                         getPnrListByEC(context);//EC接口
                         break;
                     case "getqrbysceneidTest":
                         getqrbysceneidTest(context);//测试ECRM接口
                         break;
                     default:                      
                         context.Response.Write("{\"subcode\":\"05\",\"msg\":\"请求方法错误\",\"result\":{}}");
                         break;
                 }
             }           
        }

        private void gethospqrlist(HttpContext context)
        {
            try
            {
                ComLib.LogLib.Log4NetBase.Log("1、同步HOSP二维码请求到达");
                var access_id = ComLib.QueryStringHelper.GetFormString("access_id");
                var method = ComLib.QueryStringHelper.GetFormString("method");
                var timestamp = ComLib.QueryStringHelper.GetFormString("timestamp");
                var sign = ComLib.QueryStringHelper.GetFormString("sign");

                #region 开始签名认证
                if (access_id.IsNullOrEmpty())
                {
                    context.Response.Write("{\"subcode\":\"02\",\"msg\":\"用户错误\",\"result\":{}}");
                }
                else
                {
                    #region 签名
                    var parameters = new Dictionary<string, string>();
                    parameters.Add("access_id", access_id);
                    parameters.Add("method", method);
                    parameters.Add("timestamp", timestamp);

                    var request_str = BuildQuery(parameters);
                    var signature = HMACSHA1Encrypt(request_str, "accessuser_secret_sfa");
                    string signstr = "";
                    #endregion
                    if (signature.IndexOf("+") > 0)
                    {
                        signstr = StringReplace(signature, "+", " ");//+号替换为空
                        ComLib.LogLib.Log4NetBase.Log("1-1、签名请求参数:" + request_str + "//请求的sign=" + sign + "//本地的sign=" + signstr);
                        if (sign == signstr)
                        {
                            #region 组装返回
                            #region 已弃用（查询所有）
                            //var ds = qr_QrCodeRelateService.Instance.db.From<qr_QrCodeRelate>()
                            //    .InnerJoin<qr_QrCode>(qr_QrCodeRelate._.qr_Scene_ID == qr_QrCode._.scene_id)
                            //    .Where(qr_QrCodeRelate._.qr_Type == 2).ToDataSet();
                            #endregion

                            #region 查询IsNeedGet == true
                            int SuessCount = 0;
                            int ErroeCount = 0;
                            var ds = qr_QrCodeRelateService.Instance.db.From<qr_QrCodeRelate>()
                                .InnerJoin<qr_QrCode>(qr_QrCodeRelate._.qr_Scene_ID == qr_QrCode._.scene_id)
                                .Where(qr_QrCodeRelate._.qr_Type == 2 && qr_QrCodeRelate._.IsNeedGet == true).ToDataSet();
                            ComLib.LogLib.Log4NetBase.Log("1-2、查询到数据库的HOSP数据共" + ds.Tables[0].Rows.Count + "条");
                            #endregion

                            results results = new ShowqrCode.results();
                            results.subcode = "00";
                            results.msg = "成功";

                            List<hosp> alist = new List<hosp>();
                            qr_QrCodeRelate HospRelate = new Entity.qr_QrCodeRelate();
                            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                            {
                                hosp hosp = new hosp()
                                {
                                    hospid = ds.Tables[0].Rows[i]["rel_Code"].ToString(),
                                    hospname = ds.Tables[0].Rows[i]["title"].ToString(),
                                    code_url = ds.Tables[0].Rows[i]["code_url"].ToString(),
                                    qrcode_url = ds.Tables[0].Rows[i]["qrcode_url"].ToString(),
                                };
                                alist.Add(hosp);
                                ComLib.LogLib.Log4NetBase.Log(":::HOSP当前第" + i + "条:::" + "hospid:" + hosp.hospid + ":::hospname:" + hosp.hospname + ":::code_url:" + hosp.code_url + ":::qrcode_url:" + hosp.qrcode_url);

                                #region 获取后，把IsNeedGet置为false
                                HospRelate.RelateQRID = Convert.ToInt32(ds.Tables[0].Rows[i]["RelateQRID"].ToString());
                                HospRelate.IsNeedGet = false;
                                if (qr_QrCodeRelateService.Instance.Update_Entity(HospRelate))
                                {
                                    SuessCount += 1;//成功置回
                                }
                                else
                                {
                                    ErroeCount += 1;//失败置回
                                    ComLib.LogLib.Log4NetBase.Log("1-3、HOSP还原IsNeedGet置回false失败，RelateQRID：" + HospRelate.RelateQRID);
                                }
                                #endregion
                            }
                            result result = new ShowqrCode.result()
                            {
                                countNum = ds.Tables[0].Rows.Count.ToString(),
                                alist = alist
                            };
                            results.result = result;
                            ComLib.LogLib.Log4NetBase.Log("1-3、HOSP还原IsNeedGet置回false成功:" + SuessCount + "条,失败:" + ErroeCount);
                            #endregion

                            string re = ComLib.JsonHelper.JsonSerializer<results>(results);
                            context.Response.Write(re);
                            ComLib.LogLib.Log4NetBase.Log("1-4、返回医院二维码数据:条数:" + alist.Count);
                            //    context.Response.Write("{\"subcode\":\"00\",\"msg\":\"成功\",\"result\":{\"countNum\":\"1\",\"alist\":[{\"hospid\":\"1\",\"hospname\":\"广州人民医院\",\"code_url\":\"\",\"qrcode_url\":\"\"}]}}");
                        }
                        else
                        {
                            context.Response.Write("{\"subcode\":\"03\",\"msg\":\"签名失败\",\"result\":{}}");
                            ComLib.LogLib.Log4NetBase.Log("1-2、同步医院数据错误，签名失败:::::" + "请求的签名：" + sign + ":::::" + "本地签名：" + signstr);
                        }
                    }
                    else
                    {
                        if (sign == signature)
                        {
                            #region 组装返回
                            #region 已弃用（查询所有）
                            //var ds = qr_QrCodeRelateService.Instance.db.From<qr_QrCodeRelate>()
                            //    .InnerJoin<qr_QrCode>(qr_QrCodeRelate._.qr_Scene_ID == qr_QrCode._.scene_id)
                            //    .Where(qr_QrCodeRelate._.qr_Type == 2).ToDataSet();
                            #endregion

                            #region 查询IsNeedGet == true
                            int SuessCount = 0;
                            int ErroeCount = 0;
                            var ds = qr_QrCodeRelateService.Instance.db.From<qr_QrCodeRelate>()
                                .InnerJoin<qr_QrCode>(qr_QrCodeRelate._.qr_Scene_ID == qr_QrCode._.scene_id)
                                .Where(qr_QrCodeRelate._.qr_Type == 2 && qr_QrCodeRelate._.IsNeedGet == true).ToDataSet();
                            ComLib.LogLib.Log4NetBase.Log("1-2、查询到数据库的HOSP数据共" + ds.Tables[0].Rows.Count + "条");
                            #endregion

                            results results = new ShowqrCode.results();
                            results.subcode = "00";
                            results.msg = "成功";

                            List<hosp> alist = new List<hosp>();
                            qr_QrCodeRelate HospRelate = new Entity.qr_QrCodeRelate();
                            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                            {
                                hosp hosp = new hosp()
                                {
                                    hospid = ds.Tables[0].Rows[i]["rel_Code"].ToString(),
                                    hospname = ds.Tables[0].Rows[i]["title"].ToString(),
                                    code_url = ds.Tables[0].Rows[i]["code_url"].ToString(),
                                    qrcode_url = ds.Tables[0].Rows[i]["qrcode_url"].ToString(),
                                };
                                alist.Add(hosp);
                                ComLib.LogLib.Log4NetBase.Log(":::HOSP当前第" + i + "条:::" + "hospid:" + hosp.hospid + ":::hospname:" + hosp.hospname + ":::code_url:" + hosp.code_url + ":::qrcode_url:" + hosp.qrcode_url);

                                #region 获取后，把IsNeedGet置为false
                                HospRelate.RelateQRID = Convert.ToInt32(ds.Tables[0].Rows[i]["RelateQRID"].ToString());
                                HospRelate.IsNeedGet = false;
                                if (qr_QrCodeRelateService.Instance.Update_Entity(HospRelate))
                                {
                                    SuessCount += 1;//成功置回
                                }
                                else
                                {
                                    ErroeCount += 1;//失败置回
                                    ComLib.LogLib.Log4NetBase.Log("1-3、HOSP还原IsNeedGet置回false失败，RelateQRID：" + HospRelate.RelateQRID);
                                }
                                #endregion
                            }
                            result result = new ShowqrCode.result()
                            {
                                countNum = ds.Tables[0].Rows.Count.ToString(),
                                alist = alist
                            };
                            results.result = result;
                            ComLib.LogLib.Log4NetBase.Log("1-3、HOSP还原IsNeedGet置回false成功:" + SuessCount + "条,失败:" + ErroeCount);
                            #endregion

                            string re = ComLib.JsonHelper.JsonSerializer<results>(results);
                            context.Response.Write(re);
                            ComLib.LogLib.Log4NetBase.Log("1-4、返回医院二维码数据:条数:" + alist.Count);
                            //    context.Response.Write("{\"subcode\":\"00\",\"msg\":\"成功\",\"result\":{\"countNum\":\"1\",\"alist\":[{\"hospid\":\"1\",\"hospname\":\"广州人民医院\",\"code_url\":\"\",\"qrcode_url\":\"\"}]}}");
                        }
                        else
                        {
                            context.Response.Write("{\"subcode\":\"03\",\"msg\":\"签名失败\",\"result\":{}}");
                            ComLib.LogLib.Log4NetBase.Log("1-2、同步医院数据错误，签名失败:::::" + "请求的签名：" + sign + ":::::" + "本地签名：" + signature);
                        }
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                ComLib.LogLib.Log4NetBase.Log("0-0、返回医院二维码异常：" + ex.Message + ex.StackTrace);
            }
        }

        private static void getpnrqrlist(HttpContext context)
        {
            
            try
            {
                ComLib.LogLib.Log4NetBase.Log("2、同步PNR二维码");
                var access_id = ComLib.QueryStringHelper.GetFormString("access_id");
                var method = ComLib.QueryStringHelper.GetFormString("method");
                var timestamp = ComLib.QueryStringHelper.GetFormString("timestamp");
                var sign = ComLib.QueryStringHelper.GetFormString("sign");

                #region 开始签名认证
                if (access_id.IsNullOrEmpty())
                {
                    context.Response.Write("{\"subcode\":02,\"msg\":\"用户不存在\",\"result\":{}}");
                }
                else
                {
                    var parameters = new Dictionary<string, string>();
                    parameters.Add("access_id", access_id);
                    parameters.Add("method", method);
                    parameters.Add("timestamp", timestamp);

                    var request_str = BuildQuery(parameters);
                    var signature = HMACSHA1Encrypt(request_str, "accessuser_secret_sfa");
                    string signstr = "";
                    if (signature.IndexOf("+") > 0)
                    {
                        signstr = StringReplace(signature, "+", " ");//+号替换为空

                        ComLib.LogLib.Log4NetBase.Log("2-1、签名请求参数:" + request_str + "//请求的sign=" + sign + "//本地的sign=" + signstr);
                        if (sign == signstr)
                        {
                            #region 组装返回
                            #region 已弃用（查询所有）
                            //var ds = qr_QrCodeRelateService.Instance.db.From<qr_QrCodeRelate>()
                            //    .InnerJoin<qr_QrCode>(qr_QrCodeRelate._.qr_Scene_ID == qr_QrCode._.scene_id)
                            //    .Where(qr_QrCodeRelate._.qr_Type == 3).ToDataSet();
                            #endregion

                            #region 查询IsNeedGet == true
                            int SuessCount = 0;
                            int ErroeCount = 0;
                            var ds = qr_QrCodeRelateService.Instance.db.From<qr_QrCodeRelate>()
                                .InnerJoin<qr_QrCode>(qr_QrCodeRelate._.qr_Scene_ID == qr_QrCode._.scene_id)
                                .Where(qr_QrCodeRelate._.qr_Type == 3 && qr_QrCodeRelate._.IsNeedGet == true).ToDataSet();
                            ComLib.LogLib.Log4NetBase.Log("2-2、查询到已更新的PNR数据共" + ds.Tables[0].Rows.Count + "条");
                            #endregion

                            ShareMarking.ShowqrCode_ext.results results = new ShareMarking.ShowqrCode_ext.results();
                            results.subcode = "00";
                            results.msg = "成功";

                            List<pnr> alist = new List<pnr>();
                            qr_QrCodeRelate PnrRelate = new Entity.qr_QrCodeRelate();
                            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                            {
                                pnr pnr = new pnr()
                                {
                                    pnrid = ds.Tables[0].Rows[i]["rel_Code"].ToString(),
                                    pnrname = ds.Tables[0].Rows[i]["title"].ToString(),
                                    code_url = ds.Tables[0].Rows[i]["code_url"].ToString(),
                                    qrcode_url = ds.Tables[0].Rows[i]["qrcode_url"].ToString(),
                                };
                                alist.Add(pnr);
                                ComLib.LogLib.Log4NetBase.Log(":::PNR当前第" + i + "条:::" + "pnrid:" + pnr.pnrid + ":::pnrname:" + pnr.pnrname + ":::code_url:" + pnr.code_url + ":::qrcode_url:" + pnr.qrcode_url);

                                #region 获取后，把IsNeedGet置为false
                                PnrRelate.RelateQRID = Convert.ToInt32(ds.Tables[0].Rows[i]["RelateQRID"].ToString());
                                PnrRelate.IsNeedGet = false;
                                if (qr_QrCodeRelateService.Instance.Update_Entity(PnrRelate))
                                {
                                    SuessCount += 1;//成功置回
                                }
                                else
                                {
                                    ErroeCount += 1;//失败置回
                                    ComLib.LogLib.Log4NetBase.Log("2-3、PNR还原IsNeedGet置回false失败，RelateQRID：" + PnrRelate.RelateQRID);
                                }
                                #endregion
                            }
                            ShareMarking.ShowqrCode_ext.result result = new ShareMarking.ShowqrCode_ext.result()
                            {
                                countNum = ds.Tables[0].Rows.Count.ToString(),
                                alist = alist
                            };
                            results.result = result;
                            ComLib.LogLib.Log4NetBase.Log("2-3、PNR还原IsNeedGet置回false成功:" + SuessCount + "条,失败:" + ErroeCount);
                            #endregion
                            string re = ComLib.JsonHelper.JsonSerializer<ShareMarking.ShowqrCode_ext.results>(results);
                            context.Response.Write(re);
                            ComLib.LogLib.Log4NetBase.Log("2-4、同步PNR二维码返回数据:条数:" + alist.Count);
                            //context.Response.Write("{\"subcode\":\"00\",\"msg\":\"成功\",\"result\":{\"countNum\":\"1\",\"alist\":[{\"pnrid\":\"1\",\"pnrname\":\"张为民\",\"code_url\":\"\",\"qrcode_url\":\"\"}]}}");
                        }
                        else
                        {
                            context.Response.Write("{\"subcode\":\"03\",\"msg\":\"签名失败\",\"result\":{}}");
                            ComLib.LogLib.Log4NetBase.Log("2-2、同步PNR数据错误，签名失败:::::" + "请求的签名：" + sign + ":::::" + "本地签名：" + signstr);
                        }
                    }
                    else
                    {
                        ComLib.LogLib.Log4NetBase.Log("2-1、签名请求参数:" + request_str + "//请求的sign=" + sign + "//本地的sign=" + signature);
                        if (sign == signature)
                        {
                            #region 组装返回
                            #region 已弃用（查询所有）
                            //var ds = qr_QrCodeRelateService.Instance.db.From<qr_QrCodeRelate>()
                            //    .InnerJoin<qr_QrCode>(qr_QrCodeRelate._.qr_Scene_ID == qr_QrCode._.scene_id)
                            //    .Where(qr_QrCodeRelate._.qr_Type == 3).ToDataSet();
                            #endregion

                            #region 查询IsNeedGet == true
                            int SuessCount = 0;
                            int ErroeCount = 0;
                            var ds = qr_QrCodeRelateService.Instance.db.From<qr_QrCodeRelate>()
                                .InnerJoin<qr_QrCode>(qr_QrCodeRelate._.qr_Scene_ID == qr_QrCode._.scene_id)
                                .Where(qr_QrCodeRelate._.qr_Type == 3 && qr_QrCodeRelate._.IsNeedGet == true).ToDataSet();
                            ComLib.LogLib.Log4NetBase.Log("2-2、查询到已更新的PNR数据共" + ds.Tables[0].Rows.Count + "条");
                            #endregion

                            ShareMarking.ShowqrCode_ext.results results = new ShareMarking.ShowqrCode_ext.results();
                            results.subcode = "00";
                            results.msg = "成功";

                            List<pnr> alist = new List<pnr>();
                            qr_QrCodeRelate PnrRelate = new Entity.qr_QrCodeRelate();
                            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                            {
                                pnr pnr = new pnr()
                                {
                                    pnrid = ds.Tables[0].Rows[i]["rel_Code"].ToString(),
                                    pnrname = ds.Tables[0].Rows[i]["title"].ToString(),
                                    code_url = ds.Tables[0].Rows[i]["code_url"].ToString(),
                                    qrcode_url = ds.Tables[0].Rows[i]["qrcode_url"].ToString(),
                                };
                                alist.Add(pnr);
                                ComLib.LogLib.Log4NetBase.Log(":::PNR当前第" + i + "条:::" + "pnrid:" + pnr.pnrid + ":::pnrname:" + pnr.pnrname + ":::code_url:" + pnr.code_url + ":::qrcode_url:" + pnr.qrcode_url);

                                #region 获取后，把IsNeedGet置为false
                                PnrRelate.RelateQRID = Convert.ToInt32(ds.Tables[0].Rows[i]["RelateQRID"].ToString());
                                PnrRelate.IsNeedGet = false;
                                if (qr_QrCodeRelateService.Instance.Update_Entity(PnrRelate))
                                {
                                    SuessCount += 1;//成功置回
                                }
                                else
                                {
                                    ErroeCount += 1;//失败置回
                                    ComLib.LogLib.Log4NetBase.Log("2-3、PNR还原IsNeedGet置回false失败，RelateQRID：" + PnrRelate.RelateQRID);
                                }
                                #endregion
                            }
                            ShareMarking.ShowqrCode_ext.result result = new ShareMarking.ShowqrCode_ext.result()
                            {
                                countNum = ds.Tables[0].Rows.Count.ToString(),
                                alist = alist
                            };
                            results.result = result;
                            ComLib.LogLib.Log4NetBase.Log("2-3、PNR还原IsNeedGet置回false成功:" + SuessCount + "条,失败:" + ErroeCount);
                            #endregion
                            string re = ComLib.JsonHelper.JsonSerializer<ShareMarking.ShowqrCode_ext.results>(results);
                            context.Response.Write(re);
                            ComLib.LogLib.Log4NetBase.Log("2-4、同步PNR二维码返回数据:条数:" + alist.Count);
                            //context.Response.Write("{\"subcode\":\"00\",\"msg\":\"成功\",\"result\":{\"countNum\":\"1\",\"alist\":[{\"pnrid\":\"1\",\"pnrname\":\"张为民\",\"code_url\":\"\",\"qrcode_url\":\"\"}]}}");
                        }
                        else
                        {
                            context.Response.Write("{\"subcode\":\"03\",\"msg\":\"签名失败\",\"result\":{}}");
                            ComLib.LogLib.Log4NetBase.Log("2-2、同步PNR数据错误，签名失败:::::" + "请求的签名：" + sign + ":::::" + "本地签名：" + signature);
                        }
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                ComLib.LogLib.Log4NetBase.Log("0-0、返回PNR二维码异常：" + ex.Message + ex.StackTrace);
            }
        }

        private static void getPnrListByEC(HttpContext context)
        {

            try
            {
                ComLib.LogLib.Log4NetBase.Log("1、EC获取二维码");
                var access_id = ComLib.QueryStringHelper.GetFormString("access_id");
                var method = ComLib.QueryStringHelper.GetFormString("method");
                var timestamp = ComLib.QueryStringHelper.GetFormString("timestamp");
                var sign = ComLib.QueryStringHelper.GetFormString("sign");
                //var sceneid = ComLib.QueryStringHelper.GetFormString("sceneid");
                Boolean isTest = true;
                #region 开始签名认证
                if (sign.IsNullOrEmpty())
                {
                    context.Response.Write("{\"subcode\":04,\"msg\":\"sign不能为空\",\"result\":{}}");
                    return;
                }
                if (access_id.IsNullOrEmpty())
                //if (isTest==false)
                {
                    context.Response.Write("{\"subcode\":02,\"msg\":\"用户不存在\",\"result\":{}}");
                    return;
                }
                else
                {
                    var parameters = new Dictionary<string, string>();
                    parameters.Add("access_id", access_id);
                    parameters.Add("method", method);
                    parameters.Add("timestamp", timestamp);
                    /*parameters.Add("sceneid", sceneid);*/

                    var request_str = BuildQuery(parameters);
                    var signature = HMACSHA1Encrypt(request_str, "accessuser_secret_ec");
                    string signstr = "";

                    if (signature.IndexOf("+") > 0)
                    //if (isTest==false)
                    {
                        signstr = StringReplace(signature, "+", " ");//+号替换为空
                        ComLib.LogLib.Log4NetBase.Log("2、IndexOf(+)、签名请求参数:" + request_str + "//请求的sign=" + sign + "//本地的sign=" + signstr);
                        if (sign == signstr)
                        {
                            #region 组装返回
                            
                            #region 查询所有PNR
                            DataTable dt = sfdc_PNRService.Instance.Get_Entity_byWhere_ToTable(null,sfdc_PNR._.Sync_Date__c.Desc,null);                           
                            #endregion
                            int row = dt.Rows.Count;
                            ShareMarking.ShowqrCode_ext.ecresults results = new ShareMarking.ShowqrCode_ext.ecresults();
                            results.subcode = "00";
                            results.msg = "成功";
                            ComLib.LogLib.Log4NetBase.Log("3-2、查询到二维码数据共" + row + "条");
                            List<PNR> alist = new List<PNR>();
                            for (int i = 0; i < dt.Rows.Count; i++) {  
                                PNR pnr = new PNR()
                                {
                                    PNRID=dt.Rows[i]["PNRID"].ToString(),
                                    EmployeeNumber__c = dt.Rows[i]["EmployeeNumber__c"].ToString(),
                                    Region__c = dt.Rows[i]["Region__c"].ToString(),
                                    Area__c = dt.Rows[i]["Area__c"].ToString(),
                                    Name__c = dt.Rows[i]["Name__c"].ToString(),
                                    IsActive__c =Boolean.Parse(dt.Rows[i]["IsActive__c"].ToString()),
                                    Sync_Date__c = dt.Rows[i]["Sync_Date__c"].ToString()
                                };
                                  alist.Add(pnr);                               
                                }
                                ShareMarking.ShowqrCode_ext.ecPnrResult result = new ShareMarking.ShowqrCode_ext.ecPnrResult()
                                {
                                    count = row,
                                    list = alist
                                };
                                results.result = result;
                                string re = ComLib.JsonHelper.JsonSerializer<ShareMarking.ShowqrCode_ext.ecresults>(results);
                                context.Response.Write(re);
                                ComLib.LogLib.Log4NetBase.Log("3-3、EC获取二维码成功:::结果" + re);
                           
                            #endregion
                        }
                        else
                        {
                            context.Response.Write("{\"subcode\":\"03\",\"msg\":\"签名失败\",\"result\":{}}");
                            ComLib.LogLib.Log4NetBase.Log("3-2、EC获取数据错误，签名失败:::::" + "请求的签名：" + sign + ":::::" + "本地签名：" + signstr);
                        }
                    }
                    else
                    {
                        ComLib.LogLib.Log4NetBase.Log("3-1、签名请求参数:" + request_str + "//请求的sign=" + sign + "//本地的sign=" + signature);
                        if (sign == signature)
                        {
                            #region 组装返回

                            #region 查询所有PNR
                            DataTable dt = sfdc_PNRService.Instance.Get_Entity_byWhere_ToTable(null, sfdc_PNR._.Sync_Date__c.Desc, null);
                            #endregion
                            int row = dt.Rows.Count;
                            ShareMarking.ShowqrCode_ext.ecresults results = new ShareMarking.ShowqrCode_ext.ecresults();
                            results.subcode = "00";
                            results.msg = "成功";
                            ComLib.LogLib.Log4NetBase.Log("3-2、查询到二维码数据共" + row + "条");
                            List<PNR> alist = new List<PNR>();
                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                PNR pnr = new PNR()
                                {
                                    PNRID = dt.Rows[i]["PNRID"].ToString(),
                                    EmployeeNumber__c = dt.Rows[i]["EmployeeNumber__c"].ToString(),
                                    Region__c = dt.Rows[i]["Region__c"].ToString(),
                                    Area__c = dt.Rows[i]["Area__c"].ToString(),
                                    Name__c = dt.Rows[i]["Name__c"].ToString(),
                                    IsActive__c = Boolean.Parse(dt.Rows[i]["IsActive__c"].ToString()),
                                    Sync_Date__c = dt.Rows[i]["Sync_Date__c"].ToString()
                                };
                                alist.Add(pnr);
                                //if (i == 1) {
                                //    break;//先取2条
                                //}
                            }
                            ShareMarking.ShowqrCode_ext.ecPnrResult result = new ShareMarking.ShowqrCode_ext.ecPnrResult()
                            {
                                count = row,
                                list = alist
                            };
                            results.result = result;
                            string re = ComLib.JsonHelper.JsonSerializer<ShareMarking.ShowqrCode_ext.ecresults>(results);
                            context.Response.Write(re);
                            ComLib.LogLib.Log4NetBase.Log("3-3、EC获取二维码成功:::结果" + re);

                            #endregion
                        }
                        else
                        {
                            context.Response.Write("{\"subcode\":\"03\",\"msg\":\"签名失败\",\"result\":{}}");
                            ComLib.LogLib.Log4NetBase.Log("3-2、EC获取数据错误，签名失败:::::" + "请求的签名：" + sign + ":::::" + "本地签名：" + signstr);
                        }
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                ComLib.LogLib.Log4NetBase.Log("0-0、EC获取数据异常：" + ex.Message + ex.StackTrace);
            }
        }

        /// <summary>
        /// ECRM获取子来源、指定代码
        /// </summary>
        /// <param name="context"></param>
        private static void getqrbysceneid(HttpContext context)
        {
            try
            {
                ComLib.LogLib.Log4NetBase.Log("3、ECRM获取二维码");
                var access_id =ComLib.QueryStringHelper.GetFormString("access_id");
                var method = ComLib.QueryStringHelper.GetFormString("method");
                var timestamp =ComLib.QueryStringHelper.GetFormString("timestamp");
                var sign = ComLib.QueryStringHelper.GetFormString("sign");
                var sceneid = ComLib.QueryStringHelper.GetFormString("sceneid");
                Boolean isTest = true;
                #region 开始签名认证
                if (sign.IsNullOrEmpty())
                {
                    context.Response.Write("{\"subcode\":04,\"msg\":\"sign不能为空\",\"result\":{}}");
                    return;
                }
                if (access_id.IsNullOrEmpty())
                //if (isTest==false)
                {
                    context.Response.Write("{\"subcode\":02,\"msg\":\"用户不存在\",\"result\":{}}");
                    return;
                }
                else
                {
                    var parameters = new Dictionary<string, string>();
                    parameters.Add("access_id", access_id);
                    parameters.Add("method", method);
                    parameters.Add("timestamp", timestamp);
                    parameters.Add("sceneid", sceneid);

                    var request_str = BuildQuery(parameters);
                    var signature = HMACSHA1Encrypt(request_str, "accessuser_secret_ecrm");
                    string signstr = "";
                    
                    if (signature.IndexOf("+") > 0)
                    //if (isTest==false)
                    {
                        signstr = StringReplace(signature, "+", " ");//+号替换为空
                        ComLib.LogLib.Log4NetBase.Log("3-1、IndexOf(+)、签名请求参数:" + request_str + "//请求的sign=" + sign + "//本地的sign=" + signstr);
                        if (sign == signstr)
                        {
                            #region 组装返回
                            #region （查询是否存在）
                            if (!qr_QrCodeRelateService.Instance.Get_IsExist_ByWhere(qr_QrCodeRelate._.qr_Scene_ID == sceneid && qr_QrCodeRelate._.IsDelete==false))
                            {
                                context.Response.Write("{\"subcode\":\"01\",\"msg\":\"参数错误:sceneid不存在\",\"result\":{}}");
                                ComLib.LogLib.Log4NetBase.Log("3-2、ECRM获取二维码错误，参数错误:sceneid不存在:::::" + "请求的sceneid：" + sceneid);
                            }
                            #endregion

                            #region 查询qr_Scene_ID == sceneid
                            int SuessCount = 0;
                            int ErroeCount = 0;
                            var Relate = qr_QrCodeRelateService.Instance.Get_SingleEntity_byWhere(qr_QrCodeRelate._.qr_Scene_ID == sceneid && qr_QrCodeRelate._.IsDelete == false);                            
                            #endregion

                            ShareMarking.ShowqrCode_ext.ecrmresults results = new ShareMarking.ShowqrCode_ext.ecrmresults();
                            results.subcode = "00";
                            results.msg = "成功";
                            if (Relate != null)
                            {
                                ComLib.LogLib.Log4NetBase.Log("3-2、查询到二维码数据共1条");
                                List<ECRM> alist = new List<ECRM>();
                                qr_QrCodeRelate PnrRelate = new Entity.qr_QrCodeRelate();
                                ECRM pnr = new ECRM()
                                 {
                                     source_code = Relate.Chre_Source,
                                     appointed_code = Relate.Appoint_Code
                                 };
                                alist.Add(pnr);

                                #region 获取后，把IsNeedGet置为false
                                //PnrRelate.RelateQRID = Convert.ToInt32(Relate.RelateQRID);
                                //PnrRelate.IsNeedGet = false;
                                //if (qr_QrCodeRelateService.Instance.Update_Entity(PnrRelate))
                                //{
                                //    SuessCount += 1;//成功置回
                                //}
                                //else
                                //{
                                //    ErroeCount += 1;//失败置回
                                //    ComLib.LogLib.Log4NetBase.Log("2-3、PNR还原IsNeedGet置回false失败，RelateQRID：" + PnrRelate.RelateQRID);
                                //}
                                #endregion
                                int row = 1;
                                ShareMarking.ShowqrCode_ext.ecrmresult result = new ShareMarking.ShowqrCode_ext.ecrmresult()
                            {
                                count =row,
                                list = alist
                            };
                                results.result = result;                         
                                string re = ComLib.JsonHelper.JsonSerializer<ShareMarking.ShowqrCode_ext.ecrmresults>(results);
                                context.Response.Write(re);
                                ComLib.LogLib.Log4NetBase.Log("3-3、ECRM获取二维码成功:::结果" + re);
                            }
                            else
                            {
                                context.Response.Write("{\"subcode\":\"01\",\"msg\":\"参数错误:sceneid不存在\",\"result\":{}}");
                                ComLib.LogLib.Log4NetBase.Log("3-2、ECRM获取二维码错误，Relate不存在:::::" + "请求的sceneid：" + sceneid);
                            }
                            #endregion
                        }
                        else
                        {
                            context.Response.Write("{\"subcode\":\"03\",\"msg\":\"签名失败\",\"result\":{}}");
                            ComLib.LogLib.Log4NetBase.Log("3-2、ECRM获取数据错误，签名失败:::::" + "请求的签名：" + sign + ":::::" + "本地签名：" + signstr);
                        }                           
                    }
                    else
                    {
                        ComLib.LogLib.Log4NetBase.Log("3-1、签名请求参数:" + request_str + "//请求的sign=" + sign + "//本地的sign=" + signature);
                        if (sign == signature)                        
                        //if (isTest)
                        {
                            #region 组装返回
                            #region （查询是否存在）
                            if (!qr_QrCodeRelateService.Instance.Get_IsExist_ByWhere(qr_QrCodeRelate._.qr_Scene_ID == sceneid && qr_QrCodeRelate._.IsDelete == false))
                            {
                                context.Response.Write("{\"subcode\":\"04\",\"msg\":\"参数错误:sceneid不存在\",\"result\":{}}");
                                ComLib.LogLib.Log4NetBase.Log("3-2、ECRM获取二维码错误，参数错误:sceneid不存在:::::" + "请求的sceneid：" + sceneid);
                                return;
                            }
                            #endregion

                            #region 查询qr_Scene_ID == sceneid
                            int SuessCount = 0;
                            int ErroeCount = 0;
                            var Relate = qr_QrCodeRelateService.Instance.Get_SingleEntity_byWhere(qr_QrCodeRelate._.qr_Scene_ID == sceneid && qr_QrCodeRelate._.IsDelete == false);
                            #endregion

                            ShareMarking.ShowqrCode_ext.ecrmresults results = new ShareMarking.ShowqrCode_ext.ecrmresults();
                            results.subcode = "00";
                            results.msg = "成功";
                            if (Relate != null)
                            {
                                ComLib.LogLib.Log4NetBase.Log("3-2、查询到二维码数据共1条");
                                List<ECRM> alist = new List<ECRM>();
                                qr_QrCodeRelate PnrRelate = new Entity.qr_QrCodeRelate();
                                ECRM pnr = new ECRM()
                                {
                                    source_code = Relate.Chre_Source,
                                    appointed_code = Relate.Appoint_Code
                                };
                                alist.Add(pnr);

                                #region 获取后，把IsNeedGet置为false
                                //PnrRelate.RelateQRID = Convert.ToInt32(Relate.RelateQRID);
                                //PnrRelate.IsNeedGet = false;
                                //if (qr_QrCodeRelateService.Instance.Update_Entity(PnrRelate))
                                //{
                                //    SuessCount += 1;//成功置回
                                //}
                                //else
                                //{
                                //    ErroeCount += 1;//失败置回
                                //    ComLib.LogLib.Log4NetBase.Log("2-3、PNR还原IsNeedGet置回false失败，RelateQRID：" + PnrRelate.RelateQRID);
                                //}
                                #endregion

                                int row = 1;
                                ShareMarking.ShowqrCode_ext.ecrmresult result = new ShareMarking.ShowqrCode_ext.ecrmresult()
                                {
                                    count = row,
                                    list = alist
                                };
                                results.result = result;
                                string re = ComLib.JsonHelper.JsonSerializer<ShareMarking.ShowqrCode_ext.ecrmresults>(results);
                                context.Response.Write(re);
                                ComLib.LogLib.Log4NetBase.Log("3-3、ECRM获取二维码成功:::结果" + re);
                            }
                            else
                            {
                                context.Response.Write("{\"subcode\":\"04\",\"msg\":\"参数错误:sceneid不存在\",\"result\":{}}");
                                ComLib.LogLib.Log4NetBase.Log("3-2、ECRM获取二维码错误，Relate不存在:::::" + "请求的sceneid：" + sceneid);
                                return;
                            }
                            #endregion
                        }
                        else
                        {
                            context.Response.Write("{\"subcode\":\"03\",\"msg\":\"签名失败\",\"result\":{}}");
                            ComLib.LogLib.Log4NetBase.Log("3-2、ECRM获取数据错误，签名失败:::::" + "请求的签名：" + sign + ":::::" + "本地签名：" + signstr);
                        }
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                ComLib.LogLib.Log4NetBase.Log("0-0、ECRM获取数据异常：" + ex.Message + ex.StackTrace);
            }
        }

        /// ECRM获取子来源、指定代码
        /// </summary>
        /// <param name="context"></param>
        private static void getqrbysceneidTest(HttpContext context)
        {
            try
            {
                ComLib.LogLib.Log4NetBase.Log("3、ECRM获取二维码");
                var access_id = ComLib.QueryStringHelper.GetFormString("access_id");
                var method = ComLib.QueryStringHelper.GetFormString("method");
                var timestamp = ComLib.QueryStringHelper.GetFormString("timestamp");
                var sign = ComLib.QueryStringHelper.GetFormString("sign");
                var sceneid = ComLib.QueryStringHelper.GetFormString("sceneid2");
                Boolean isTest = true;
                #region 开始签名认证
                //if (access_id.IsNullOrEmpty())
                if (isTest == false)
                {
                    context.Response.Write("{\"subcode\":02,\"msg\":\"用户不存在\",\"result\":{}}");
                }
                else
                {
                    var parameters = new Dictionary<string, string>();
                    parameters.Add("access_id", access_id);
                    parameters.Add("method", method);
                    parameters.Add("timestamp", timestamp);
                    parameters.Add("sceneid", sceneid);

                    var request_str = BuildQuery(parameters);
                    var signature = HMACSHA1Encrypt(request_str, "accessuser_secret_ecrm");
                    string signstr = "";

                    //if (signature.IndexOf("+") > 0)
                    if (isTest == false)
                    {
                        signstr = StringReplace(signature, "+", " ");//+号替换为空
                        ComLib.LogLib.Log4NetBase.Log("3-1、签名请求参数:" + request_str + "//请求的sign=" + sign + "//本地的sign=" + signstr);
                        if (sign == signstr)
                        {
                            #region 组装返回
                            #region （查询是否存在）
                            if (!qr_QrCodeRelateService.Instance.Get_IsExist_ByWhere(qr_QrCodeRelate._.qr_Scene_ID == sceneid && qr_QrCodeRelate._.IsDelete == false))
                            {
                                context.Response.Write("{\"subcode\":\"01\",\"msg\":\"参数错误:sceneid不存在\",\"result\":{}}");
                                ComLib.LogLib.Log4NetBase.Log("3-2、ECRM获取二维码错误，参数错误:sceneid不存在:::::" + "请求的sceneid：" + sceneid);
                            }
                            #endregion

                            #region 查询qr_Scene_ID == sceneid
                            int SuessCount = 0;
                            int ErroeCount = 0;
                            var Relate = qr_QrCodeRelateService.Instance.Get_SingleEntity_byWhere(qr_QrCodeRelate._.qr_Scene_ID == sceneid && qr_QrCodeRelate._.IsDelete == false);
                            #endregion

                            ShareMarking.ShowqrCode_ext.ecrmresults results = new ShareMarking.ShowqrCode_ext.ecrmresults();
                            results.subcode = "00";
                            results.msg = "成功";
                            if (Relate != null)
                            {
                                ComLib.LogLib.Log4NetBase.Log("3-2、查询到二维码数据共1条");
                                List<ECRM> alist = new List<ECRM>();
                                qr_QrCodeRelate PnrRelate = new Entity.qr_QrCodeRelate();
                                ECRM pnr = new ECRM()
                                {
                                    source_code = Relate.Chre_Source,
                                    appointed_code = Relate.Appoint_Code
                                };
                                alist.Add(pnr);

                                #region 获取后，把IsNeedGet置为false
                                //PnrRelate.RelateQRID = Convert.ToInt32(Relate.RelateQRID);
                                //PnrRelate.IsNeedGet = false;
                                //if (qr_QrCodeRelateService.Instance.Update_Entity(PnrRelate))
                                //{
                                //    SuessCount += 1;//成功置回
                                //}
                                //else
                                //{
                                //    ErroeCount += 1;//失败置回
                                //    ComLib.LogLib.Log4NetBase.Log("2-3、PNR还原IsNeedGet置回false失败，RelateQRID：" + PnrRelate.RelateQRID);
                                //}
                                #endregion
                                int row = 1;
                                ShareMarking.ShowqrCode_ext.ecrmresult result = new ShareMarking.ShowqrCode_ext.ecrmresult()
                                {
                                    count = row,
                                    list = alist
                                };
                                results.result = result;
                                string re = ComLib.JsonHelper.JsonSerializer<ShareMarking.ShowqrCode_ext.ecrmresults>(results);
                                context.Response.Write(re);
                                ComLib.LogLib.Log4NetBase.Log("3-3、ECRM获取二维码成功:::结果" + re);
                            }
                            else
                            {
                                context.Response.Write("{\"subcode\":\"01\",\"msg\":\"参数错误:sceneid不存在\",\"result\":{}}");
                                ComLib.LogLib.Log4NetBase.Log("3-2、ECRM获取二维码错误，Relate不存在:::::" + "请求的sceneid：" + sceneid);
                            }
                            #endregion
                        }
                        else
                        {
                            context.Response.Write("{\"subcode\":\"03\",\"msg\":\"签名失败\",\"result\":{}}");
                            ComLib.LogLib.Log4NetBase.Log("3-2、ECRM获取数据错误，签名失败:::::" + "请求的签名：" + sign + ":::::" + "本地签名：" + signstr);
                        }
                    }
                    else
                    {
                        ComLib.LogLib.Log4NetBase.Log("3-1、签名请求参数:" + request_str + "//请求的sign=" + sign + "//本地的sign=" + signstr);
                        //if (sign == signstr)

                        if (isTest)
                        {
                            #region 组装返回
                            #region （查询是否存在）
                            if (!qr_QrCodeRelateService.Instance.Get_IsExist_ByWhere(qr_QrCodeRelate._.qr_Scene_ID == sceneid && qr_QrCodeRelate._.IsDelete == false))
                            {
                                context.Response.Write("{\"subcode\":\"04\",\"msg\":\"参数错误:sceneid不存在\",\"result\":{}}");
                                ComLib.LogLib.Log4NetBase.Log("3-2、ECRM获取二维码错误，参数错误:sceneid不存在:::::" + "请求的sceneid：" + sceneid);
                                return;
                            }
                            #endregion

                            #region 查询qr_Scene_ID == sceneid
                            int SuessCount = 0;
                            int ErroeCount = 0;
                            var Relate = qr_QrCodeRelateService.Instance.Get_SingleEntity_byWhere(qr_QrCodeRelate._.qr_Scene_ID == sceneid && qr_QrCodeRelate._.IsDelete == false);
                            #endregion

                            ShareMarking.ShowqrCode_ext.ecrmresults results = new ShareMarking.ShowqrCode_ext.ecrmresults();
                            results.subcode = "00";
                            results.msg = "成功";
                            if (Relate != null)
                            {
                                ComLib.LogLib.Log4NetBase.Log("3-2、查询到二维码数据共1条");
                                List<ECRM> alist = new List<ECRM>();
                                qr_QrCodeRelate PnrRelate = new Entity.qr_QrCodeRelate();
                                ECRM pnr = new ECRM()
                                {
                                    source_code = Relate.Chre_Source,
                                    appointed_code = Relate.Appoint_Code
                                };
                                alist.Add(pnr);

                                #region 获取后，把IsNeedGet置为false
                                //PnrRelate.RelateQRID = Convert.ToInt32(Relate.RelateQRID);
                                //PnrRelate.IsNeedGet = false;
                                //if (qr_QrCodeRelateService.Instance.Update_Entity(PnrRelate))
                                //{
                                //    SuessCount += 1;//成功置回
                                //}
                                //else
                                //{
                                //    ErroeCount += 1;//失败置回
                                //    ComLib.LogLib.Log4NetBase.Log("2-3、PNR还原IsNeedGet置回false失败，RelateQRID：" + PnrRelate.RelateQRID);
                                //}
                                #endregion

                                int row = 1;
                                ShareMarking.ShowqrCode_ext.ecrmresult result = new ShareMarking.ShowqrCode_ext.ecrmresult()
                                {
                                    count = row,
                                    list = alist
                                };
                                results.result = result;
                                string re = ComLib.JsonHelper.JsonSerializer<ShareMarking.ShowqrCode_ext.ecrmresults>(results);
                                context.Response.Write(re);
                                ComLib.LogLib.Log4NetBase.Log("3-3、ECRM获取二维码成功:::结果" + re);
                            }
                            else
                            {
                                context.Response.Write("{\"subcode\":\"04\",\"msg\":\"参数错误:sceneid不存在\",\"result\":{}}");
                                ComLib.LogLib.Log4NetBase.Log("3-2、ECRM获取二维码错误，Relate不存在:::::" + "请求的sceneid：" + sceneid);
                                return;
                            }
                            #endregion
                        }
                        else
                        {
                            context.Response.Write("{\"subcode\":\"03\",\"msg\":\"签名失败\",\"result\":{}}");
                            ComLib.LogLib.Log4NetBase.Log("3-2、ECRM获取数据错误，签名失败:::::" + "请求的签名：" + sign + ":::::" + "本地签名：" + signstr);
                        }
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                ComLib.LogLib.Log4NetBase.Log("0-0、ECRM获取数据异常：" + ex.Message + ex.StackTrace);
            }
        }

        #region 测试医院接口方法
        private void gethospqrlist2(HttpContext context)
        {
            try
            {
                ComLib.LogLib.Log4NetBase.Log("1、测试同步HOSP二维码请求到达");
                #region 开始签名认证

                #region 组装返回
                #region 已弃用（查询所有）
                //var ds = qr_QrCodeRelateService.Instance.db.From<qr_QrCodeRelate>()
                //    .InnerJoin<qr_QrCode>(qr_QrCodeRelate._.qr_Scene_ID == qr_QrCode._.scene_id)
                //    .Where(qr_QrCodeRelate._.qr_Type == 2).ToDataSet();
                #endregion

                #region 查询IsNeedGet == true
                int SuessCount = 0;
                int ErroeCount = 0;
                var ds = qr_QrCodeRelateService.Instance.db.From<qr_QrCodeRelate>()
                    .InnerJoin<qr_QrCode>(qr_QrCodeRelate._.qr_Scene_ID == qr_QrCode._.scene_id)
                    .Where(qr_QrCodeRelate._.qr_Type == 2 && qr_QrCodeRelate._.IsNeedGet == true).ToDataSet();
                ComLib.LogLib.Log4NetBase.Log("1-2、查询到已更新的HOSP数据共" + ds.Tables[0].Rows.Count + "条");
                #endregion

                results results = new ShowqrCode.results();
                results.subcode = "00";
                results.msg = "成功";

                List<hosp> alist = new List<hosp>();
                qr_QrCodeRelate Relate = new Entity.qr_QrCodeRelate();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    hosp hosp = new hosp()
                    {
                        hospid = ds.Tables[0].Rows[i]["rel_Code"].ToString(),
                        hospname = ds.Tables[0].Rows[i]["title"].ToString(),
                        code_url = ds.Tables[0].Rows[i]["code_url"].ToString(),
                        qrcode_url = ds.Tables[0].Rows[i]["qrcode_url"].ToString(),
                    };
                    alist.Add(hosp);

                    #region 获取后，把IsNeedGet置为false（测试接口不还原,）   
                     SuessCount += 1;//成功置回
                    //Relate.RelateQRID = Convert.ToInt32(ds.Tables[0].Rows[i]["RelateQRID"].ToString());
                    //Relate.IsNeedGet = false;
                    //if (qr_QrCodeRelateService.Instance.Update_Entity(Relate))
                    //{
                    //    SuessCount += 1;//成功置回
                    //}
                    //else
                    //{
                    //    ErroeCount += 1;//失败置回
                    //    ComLib.LogLib.Log4NetBase.Log("1-3、HOSP还原IsNeedGet置回false失败，RelateQRID：" + Relate.RelateQRID);
                    //}                    
                     #endregion
                }
                result result = new ShowqrCode.result()
                {
                    countNum = ds.Tables[0].Rows.Count.ToString(),
                    alist = alist
                };
                results.result = result;
                ComLib.LogLib.Log4NetBase.Log("1-3、HOSP还原IsNeedGet置回false成功:" + SuessCount + "条,失败:" + ErroeCount);
                #endregion

                string re = ComLib.JsonHelper.JsonSerializer<results>(results);
                context.Response.Write(re);
                ComLib.LogLib.Log4NetBase.Log("1-4、返回医院二维码数据:条数:" + alist.Count + "========HOSP详情记录:" + re);
                
                #endregion              
            }
                
            catch (Exception ex)
            {
                ComLib.LogLib.Log4NetBase.Log("0-0、返回医院二维码异常：" + ex.Message + ex.StackTrace);
            }
        }
        #endregion

        #region 测试PNR接口方法
        private static void getpnrqrlist2(HttpContext context)
        {
            ComLib.LogLib.Log4NetBase.Log("2、测试同步PNR二维码请求到达");
            try
            {
                #region 开始签名认证
                #region 组装返回
                #region 已弃用（查询所有）
                //var ds = qr_QrCodeRelateService.Instance.db.From<qr_QrCodeRelate>()
                //    .InnerJoin<qr_QrCode>(qr_QrCodeRelate._.qr_Scene_ID == qr_QrCode._.scene_id)
                //    .Where(qr_QrCodeRelate._.qr_Type == 3).ToDataSet();
                #endregion

                #region 查询IsNeedGet == true
                int SuessCount = 0;
                int ErroeCount = 0;
                var ds = qr_QrCodeRelateService.Instance.db.From<qr_QrCodeRelate>()
                    .InnerJoin<qr_QrCode>(qr_QrCodeRelate._.qr_Scene_ID == qr_QrCode._.scene_id)
                    .Where(qr_QrCodeRelate._.qr_Type == 3 && qr_QrCodeRelate._.IsNeedGet == true).ToDataSet();
                ComLib.LogLib.Log4NetBase.Log("2-2、查询到已更新的PNR数据共" + ds.Tables[0].Rows.Count + "条");
                #endregion

                ShareMarking.ShowqrCode_ext.results results = new ShareMarking.ShowqrCode_ext.results();
                results.subcode = "00";
                results.msg = "成功";

                List<pnr> alist = new List<pnr>();
                qr_QrCodeRelate Relate2 = new Entity.qr_QrCodeRelate();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    pnr pnr = new pnr()
                    {
                        pnrid = ds.Tables[0].Rows[i]["rel_Code"].ToString(),
                        pnrname = ds.Tables[0].Rows[i]["title"].ToString(),
                        code_url = ds.Tables[0].Rows[i]["code_url"].ToString(),
                        qrcode_url = ds.Tables[0].Rows[i]["qrcode_url"].ToString(),
                    };
                    alist.Add(pnr);

                    #region 获取后，把IsNeedGet置为false(测试接口不还原)
                    SuessCount += 1;//成功置回
                    //Relate2.RelateQRID = Convert.ToInt32(ds.Tables[0].Rows[i]["RelateQRID"].ToString());
                    //Relate2.IsNeedGet = false;
                    //if (qr_QrCodeRelateService.Instance.Update_Entity(Relate2))
                    //{
                    //    SuessCount += 1;//成功置回
                    //}
                    //else
                    //{
                    //    ErroeCount += 1;//失败置回
                    //    ComLib.LogLib.Log4NetBase.Log("2-3、PNR还原IsNeedGet置回false失败，RelateQRID：" + Relate2.RelateQRID);
                    //}
                    #endregion
                }
                ShareMarking.ShowqrCode_ext.result result = new ShareMarking.ShowqrCode_ext.result()
                {
                    countNum = ds.Tables[0].Rows.Count.ToString(),
                    alist = alist
                };
                results.result = result;
                ComLib.LogLib.Log4NetBase.Log("2-3、PNR还原IsNeedGet置回false成功:" + SuessCount + "条,失败:" + ErroeCount);
                #endregion
                string re = ComLib.JsonHelper.JsonSerializer<ShareMarking.ShowqrCode_ext.results>(results);
                context.Response.Write(re);
                ComLib.LogLib.Log4NetBase.Log("2-4、同步PNR二维码返回数据:条数:" + alist.Count + "========PNR详情记录:" + re);
                #endregion
            }
            catch (Exception ex)
            {
                ComLib.LogLib.Log4NetBase.Log("0-0、返回PNR二维码异常：" + ex.Message + ex.StackTrace);
            }
        }

        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public static string BuildQuery(IDictionary<string, string> parameters)
        {
            StringBuilder postData = new StringBuilder();
            bool hasParam = false;

            IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator();
            while (dem.MoveNext())
            {
                string name = dem.Current.Key;
                string value = dem.Current.Value;
                // 忽略参数名为空的参数
                if (!string.IsNullOrEmpty(name))
                {
                    if (hasParam)
                    {
                        postData.Append("&");
                    }

                    //postData.Append(HttpUtility.UrlEncode(name, Encoding.UTF8));
                    //postData.Append("=");
                    //postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8));
                    postData.Append(name);
                    postData.Append("=");
                    postData.Append(value);
                    hasParam = true;
                }
            }

            return postData.ToString();
        }

        public static string HMACSHA1Encrypt(string EncryptText, string EncryptKey)
        {
            HMACSHA1 hmacsha1 = new HMACSHA1();
            hmacsha1.Key = Encoding.UTF8.GetBytes(EncryptKey);
            byte[] dataBuffer = Encoding.UTF8.GetBytes(EncryptText);
            byte[] hashBytes = hmacsha1.ComputeHash(dataBuffer);
            return Convert.ToBase64String(hashBytes);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="str">文本</param>
        /// <param name="toRep">替换文本</param>
        /// <param name="strRep">替换成文本</param>
        /// <returns></returns>
        public static string StringReplace(string str, string toRep, string strRep)
        {
            StringBuilder sb = new StringBuilder();
            int np = 0, n_ptmp = 0;
            for (; ; )
            {
                string str_tmp = str.Substring(np);
                n_ptmp = str_tmp.IndexOf(toRep);

                if (n_ptmp == -1)
                {
                    sb.Append(str_tmp);
                    break;
                }
                else
                {
                    sb.Append(str_tmp.Substring(0, n_ptmp)).Append(strRep);
                    np += n_ptmp + toRep.Length;
                }
            }
            return sb.ToString();
        }    
    }

    public class results
    {
        public string subcode;
        public string msg;
        public result result;
    }
    public class result
    {
        public string countNum;
        public List<hosp> alist;
    }

    public class hosp
    {
        public string hospid;
        public string hospname;
        public string code_url;
        public string qrcode_url;
    }

    public class pnr
    {
        public string pnrid;
        public string pnrname;
        public string code_url;
        public string qrcode_url;
    }
    public class ECRM
    {       
        /// <summary>
        /// 子来源
        /// </summary>
        public string source_code;
        /// <summary>
        /// 指定代码  
        /// </summary>
        public string appointed_code;  
    }
    public class PNR
    {
        /// <summary>
        /// PNRid
        /// </summary>
        public string PNRID;
        /// <summary>
        /// PNR编码  
        /// </summary>
        public string EmployeeNumber__c;       
        /// <summary>
        /// PNR大区  
        /// </summary>
        public string Region__c;
        /// <summary>
        /// PNR片区
        /// </summary>
        public string Area__c;
        /// <summary>
        /// PNR姓名  
        /// </summary>
        public string Name__c;       
        /// <summary>
        /// PNR是否已激活 
        /// </summary>
        public Boolean IsActive__c;      
        /// <summary>
        /// 同步时间
        /// </summary>
        public String Sync_Date__c;
        
    }   
}

namespace ShareMarking.ShowqrCode_ext
{
    public class results
    {
        public string subcode;
        public string msg;
        public result result;
    }
    public class ecrmresults
    {
        public string subcode;
        public string msg;
        public ecrmresult result;
    }
    public class ecresults
    {
        public string subcode;
        public string msg;
        public ecPnrResult result;
    }
    public class result
    {
        public string countNum;
        public List<ShareMarking.ShowqrCode.pnr> alist;
    }

    public class ecrmresult
    {
        public int count;
        public List<ShareMarking.ShowqrCode.ECRM> list;
    }
    public class ecPnrResult
    {
        public int count;
        public List<ShareMarking.ShowqrCode.PNR> list;
    }
}